<?php 

class carModel extends appModel
{
	
    /*
     * @author zzx
     * @desc 获取中奖信息 
     */
    /* public function getPrize($openid)
    {
        $prize = array();
        $id = 0;
        $user_id = 0;
        $result = array();
        // 中奖逻辑
        $sql = "SELECT * FROM ".$this->table('chezhan_prize').' ORDER BY pri';
        $res = $this->getAll($sql);
        if($res){
            foreach($res as $k =>$v){
                
                if(rand(1,100) < $v['pby']){
                    $id = $v['id'];
                    break;
                } 
            }
        }
        
       
        
        if($id!=0){
            $sql2 = "SELECT id FROM ".$this->table('chezhan_prize_user')." WHERE pid='".$id."' and openid='".$openid."'";
            $res2 = $this->getRow($sql2);
            if($res2 && $res2['is_card']==1){
                $id = 0;
            }else{
                $sql3 = "SELECT * FROM ".$this->table('chezhan_prize_period').' WHERE begintime<'.time().' and endtime>'.time().' and nums>prizenums and pid='.$id;
                $result = $this->getRow($sql3);
                
                if($result){
                    $pnum = $result['prizenums']+1;
                    $sql4  = "UPDATE ".$this->table('chezhan_prize_period').' set prizenums ='.$pnum.' where id='.$result['id'];
                    $res4 = $this->query($sql4);
                    if(!$res2){
                        $sql5 = 'INSERT INTO '.$this->table('chezhan_prize_user').'(pid,ppid,openid,addtime) VALUES ("'.$id.'","'.$result['id'].'","'.$openid.'","'.time().'")';
                        $res3 = $this->query($sql5);
                        $user_id = $this->insert_id();
                    }else{
                        $user_id = $res2['id'];
                    }
                    
                }else{
                    $id = 0;
                }
            }
            
        }
        
        
        
        //获取中奖的记录
        if($id != 0){
            $sql6 = 'SELECT * FROM '.$this->table('chezhan_prize').' WHERE id="'.$id.'"';
            $prize = $this->getRow($sql6);
            
        }else{
            $prize['id']=0;
        }
        $prize['user_id'] = $user_id;
        
        return $prize;
    } */
    
    public function getPrize($openid)
    {
        $prize = array();
        $id = 0;
        $user_id = 0;
        $where = ' WHERE 1=1 ';
        $result = "";
        // 中奖逻辑
        
        $sql_1 = 'SELECT addtime FROM '.$this->table('chezhan_prize_user').' WHERE openid="'.$openid.'" ORDER BY addtime desc';
        $res_1 = $this->getOne($sql_1);
        if(!$res_1){
            $res_1 = 0;
        }
        $res_1 = date("Y-m-d",$res_1);
        $new = date("Y-m-d",time());
        if($res_1 != $new){
        
        
            $sql3 = 'SELECT pid,id,prizenums,nums FROM '.$this->table('chezhan_prize_period').' WHERE begintime<'.time().' and endtime>'.time().' and nums>prizenums';
            $result3 = $this->getAll($sql3);
            
            
            
            if($result3){
                
                
                
                $ids = '(';
                foreach($result3 as $k =>$v){
                    
                    
                    
                    $ids.=$v['pid'].',';
                }
                $ids = substr($ids,0,strlen($ids)-1);
                $ids.=')';
                $where.= ' AND id in '.$ids;
            
            
                $sql = 'SELECT * FROM '.$this->table('chezhan_prize').$where.' ORDER BY pri';
                $res = $this->getAll($sql);
                if($res){
                    foreach($res as $k =>$v){
            
                        if(rand(1,100) < $v['pby']){
                            $id = $v['id'];
                            break;
                        }
                    }
                }
                if($id!=0){
                    
                    $sql2 = 'SELECT id FROM '.$this->table('chezhan_prize_user').' WHERE pid="'.$id.'" and openid="'.$openid.'"';
                    $res2 = $this->getRow($sql2);
                    if($res2 && $res2['is_card']==1){
                        $id = 0;
                    }else{
                        
                        foreach($result3 as $k =>$v){
                            if($id==$result3[$k]['pid']){
                                $pnum = $result3[$k]['prizenums']+1;
                                $result = $result3[$k]['id'];
                            }
                        }
                        
                        $sql7 = 'SELECT prizenums FROM '.$this->table('chezhan_prize').' WHERE id="'.$id.'"';
                        $res7 = $this->getOne($sql7);
                        $res7 = $res7+1;
                        $sql8 = 'UPDATE '.$this->table('chezhan_prize').' set prizenums ="'.$res7.'" where id="'.$id.'"';
                        $res8 = $this->query($sql8);
                        
                        
                        
                        $sql4  = 'UPDATE '.$this->table('chezhan_prize_period').' set prizenums ="'.$pnum.'" where id="'.$result.'"';
                        $res4 = $this->query($sql4);
                        if(!$res2){
                            $sql5 = 'INSERT INTO '.$this->table('chezhan_prize_user').'(pid,ppid,openid,addtime) VALUES ("'.$id.'","'.$result.'","'.$openid.'","'.time().'")';
                            $res3 = $this->query($sql5);
                            $user_id = $this->insert_id();
                        }else{
                            $user_id = $res2['id'];
                        }
            
                    }
            
                }
            }
        }
    
        //获取中奖的记录
        if($id != 0){
            $sql6 = 'SELECT * FROM '.$this->table('chezhan_prize').' WHERE id="'.$id.'"';
            $prize = $this->getRow($sql6);
    
        }else{
            $prize['id']=0;
        }
        $prize['user_id'] = $user_id;
    
        return $prize;
    }
    
    public function getCard($prize_user)
    {
        $sql = " SELECT p2.card_id FROM ".$this->table("chezhan_prize_user")." AS p1 "
              ." LEFT JOIN ".$this->table("chezhan_prize")." AS p2 ON p1.pid = p2.id  "   
              ." WHERE p1.id ='".$prize_user."'";
        
        $back = $this->getOne($sql);
        
        
        
//      $back = "plHTpwmlhQqJ_6itfucIAlIcADaI";
        
        if($back)
        {
            
            $card_list[]['cardId'] = $back;
            
            return $card_list;
        }
        else
        {
            return array();
        }
    }
    
	public function saveCard($id)
	{
	    
	    $this->setInc("is_card", 1, "chezhan_prize_user", $id);
	    
	}
	
	public function setInc($field,$step,$table,$id)
	{
	
	
	    $sql = " update ".$this->table($table)." set ".$field." = ".$field." + ".$step." where id = '".$id."'";
	
	    return $this->query($sql);
	}
	
	/*
	 * @author zzx
	 * @desc 获取中奖信息记录
	 */
	public function getPrizeUserRecord($prize_user_id)
	{
        $prize_user = array();
        
        $sql = " SELECT p1.id,p2.support,p2.prize_name,p2.money,p1.is_card FROM ".$this->table("chezhan_prize_user")." AS p1 "
            ." LEFT JOIN ".$this->table("chezhan_prize")." AS p2 ON p1.pid = p2.id  "
                ." WHERE p1.id ='".$prize_user_id."'";
        
        $prize_user = $this->getRow($sql);
        
//         var_dump($prize_user);
        
        return $prize_user;
	}
	
	
}

?>
